package com.gxuwz.stats.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxuwz.stats.entity.CreativeAchievement;
import com.gxuwz.stats.entity.DecisionConsulting;
import com.gxuwz.stats.entity.User;
import com.gxuwz.stats.mapper.CreativeAchievementMapper;
import com.gxuwz.stats.service.CreativeAchievementService;
import com.gxuwz.stats.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CreativeAchievementServiceImpl
        extends ServiceImpl<CreativeAchievementMapper, CreativeAchievement>
        implements CreativeAchievementService {

    @Autowired
    private UserService userService;

    /**
     * 分页查询并补充用户名
     */
    public IPage<CreativeAchievement> getCreativeAchievementList(IPage<CreativeAchievement> page, QueryWrapper<CreativeAchievement> queryWrapper) {
        IPage<CreativeAchievement> resultPage = this.page(page, queryWrapper);
        // 补充用户名信息
        for (CreativeAchievement creative  : resultPage.getRecords()) {
            Integer userId = creative .getUserId();
            if (userId != null) {
                User user = userService.getById(userId);
                if (user != null) {
                    creative.setUsername(user.getUsername());
                }
            }
        }
        return resultPage;
    }

    @Override
    public IPage<CreativeAchievement> page(Page<CreativeAchievement> page, QueryWrapper<CreativeAchievement> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }


    /**
     * 条件查询列表
     */
    @Override
    public List<CreativeAchievement> list(QueryWrapper<CreativeAchievement> queryWrapper) {
        return  baseMapper.selectList(queryWrapper);
//        // 补充用户名信息
//        for (CreativeAchievement creative : list) {
//            Integer userId = creative.getUserId();
//            if (userId != null) {
//                User user = userService.getById(userId);
//                if (user != null) {
//                    creative.setUsername(user.getUsername());
//                }
//            }
//        }
//        return list;
    }

    /**
     * 自定义保存方法（带用户ID）
     */
    @Override
    public boolean save(CreativeAchievement creativeAchievement) {
        return baseMapper.insertWithUserId(creativeAchievement) > 0;
    }

    /**
     * 根据ID删除
     */
    @Override
    public boolean removeById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 批量删除
     */
    @Override
    public boolean removeByIds(List<Long> ids) {
        QueryWrapper<CreativeAchievement> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("creative_id", ids);
        return baseMapper.delete(queryWrapper) > 0;
    }

    /**
     * 根据ID查询
     */
    @Override
    public CreativeAchievement getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据ID更新
     */
//    @Override
//    public boolean updateById(CreativeAchievement creativeAchievement) {
//        return baseMapper.updateById(creativeAchievement) > 0;
//    }
}
